package yunhe.servlet;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import yunhe.biz.ISaleBiz;
import yunhe.biz.impl.SaleBizImpl;
import yunhe.entity.Sale;
import yunhe.util.GeneralUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/6/20.
 */
@WebServlet(name = "SaleListQueryByDateServlet",urlPatterns = "/sale/SaleListQueryByDateServlet")
public class SaleListQueryByDateServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Date startDate=GeneralUtil.stringToUtilDate( request.getParameter("startDate"),"yy-MM-dd");
        Date endDate=GeneralUtil.stringToUtilDate( request.getParameter("endDate"),"yy-MM-dd");

        ISaleBiz iSaleBizImpl = new SaleBizImpl();
        List<Sale> list = iSaleBizImpl.saleListBySaleDate(startDate,endDate);
        JSONArray ja=new JSONArray();
        for(Sale s:list){
            JSONObject jb=new JSONObject(s);
            try {
                jb.put("cusId",s.getCustomer().getCusId());
                jb.put("workId",s.getEmpMsg().getWorkId());
            }catch (JSONException e){
                e.printStackTrace();
            }
            ja.put(jb);
            System.out.println(ja.toString());
        }
        response.getWriter().print(ja.toString());
    }

    }

